<?php
	namespace Qzn\View;
	
	class ProfileView {
		
		private $m_pwResetButton = "pwResetButton";
		private $m_getNewPassword = "newPassword";
		private $m_getNewPasswordAgain = "newPasswordAgain";
		private $m_getOldPassword = "oldPassword";
		private $m_pwShowResetButton = "pwShowResetButton";
		private $m_addFriendButton = "Add Friend";
		
		/**
		 * @return string $html
		 */
		public function DoMyProfileBox(\Qzn\Model\ProfileHandler $ph, \Qzn\View\LoginView $lw, \Qzn\Model\LoginHandler $lh) {
			$html = "";
			$html .= $this->DoProfileBox($ph, $lw, $lh);
			if (!$this->TriedToPressShowResetBox()) {
				$html .= "<form method='post'>
							<fieldset>
								<input name='$this->m_pwShowResetButton' type='submit' value='Change Password'>
							</fieldset>
						</form>";
			}
			
			return $html;
		}
		
		/**
		 * @return string $html
		 */
		public function DoProfileBox(\Qzn\Model\ProfileHandler $ph, \Qzn\View\LoginView $lw, \Qzn\Model\LoginHandler $lh) {
			$userPk = \Qzn\View\NavigationView::getProfilePk();
			
			$pd = $ph->GetProfileData($userPk);
			$pp = $ph->GetPicturePath($userPk);
			
			if ($pd[0] !== null) {
				$html = "<div>
							<p><a href='?profile=$userPk'>$pd[0]</a></p>
							<img src='$pp' alt='Profile Picture' />
							<p>Join date: $pd[1]</p>
							<p>User: $pd[2]</p>";
				$fs = $ph->GetFriendship($userPk, $lw);
				if ($lw->GetUserPk() != $userPk && $lh->IsLoggedIn()) {
					if (count($fs) == 0 || count($fs) <= 1 && $fs[0] != 1 && $fs[0] != 3) {
						$html .= "<form method='post'>
									<fieldset>
										<input name='$userPk' type='submit' value='$this->m_addFriendButton'>
									</fieldset>
								</form>";
					}
				} else if ($lw->GetUserPk() == $userPk && $lh->IsLoggedIn()) {
					$html .= "<p><a href='?fileupload'>Change profile picture!</a></p>";
				}
				$html .= "</div>";
			} else {
				$html = "<p>This user does not exist</p>";
			}
			
			return $html;
		}
		
		/**
		 * @return string $html
		 */
		public function DoResetPasswordBox() {
			$html = "<form method='post'>
						<fieldset>
							<label for='$this->m_getOldPassword'>Old Password: </label>
							<input name='$this->m_getOldPassword' type='password'/><br />
							<label for='$this->m_getNewPassword'>New Password: </label>
							<input name='$this->m_getNewPassword' type='password'/><br />
							<label for='$this->m_getNewPasswordAgain'>New Password Again: </label>
							<input name='$this->m_getNewPasswordAgain' type='password'/>
							<input name='$this->m_pwResetButton' type='submit' value='Change Password'>
						</fieldset>
					</form>";
			
			return $html;
		}
		
		/**
		 * @param int $errorCode
		 * @return string $html
		 */
		public function ErrorHandler($errorCode) {
			switch ($errorCode) {
				case \Qzn\Model\profileHandler::EVERYTHING_WENT_BETTER_THEN_EXPECTED:
						$html = "Password change successfull!";
					break;
				case \Qzn\Model\profileHandler::INVALID_OLD_PASSWORD:
						$html = "Error: Invalid old password.";
					break;
				case \Qzn\Model\profileHandler::INVALID_NEW_PASSWORD:
						$html = "Error: Invalid new password, requires a minimum of 6 characters.";
					break;
				case \Qzn\Model\profileHandler::INVALID_NEW_PASSWORD_NO_MATCH:
						$html = "Error: The passwords does not match.";
					break;
				case \Qzn\Model\profileHandler::INVALID_USER:
						$html = "Error: This is not your password.";
					break;
			}
			return $html;
		}
		
		/**
		 * @return string @pw
		 */
		public function GetOldPasswordText() {
			if(isset($_POST[$this->m_getOldPassword])) {
				return $_POST[$this->m_getOldPassword];
			}
			return NULL;
		}
		
		/**
		 * @return string @pw
		 */
		public function GetNewPasswordTextAgain() {
			if(isset($_POST[$this->m_getNewPasswordAgain])) {
				return $_POST[$this->m_getNewPasswordAgain];
			}
			return NULL;
		}
		
		/**
		 * @return string @pw
		 */
		public function GetNewPasswordText() {
			if(isset($_POST[$this->m_getNewPassword])) {
				return $_POST[$this->m_getNewPassword];
			}
			return NULL;
		}
		
		/**
		 * @return boolean addFriend
		 */
		public function TriedToPressAddFriend() {
			if (count(array_keys($_POST, $this->m_addFriendButton)) == 1) {
				return true;
			}
			
			return false;
		}
		
		/**
		 * @return string $ret
		 */
		public function GetAddFriend() {
			if (count(array_keys($_POST, $this->m_addFriendButton)) == 1) {
				$ret = array_keys($_POST, $this->m_addFriendButton);
				$ret = end($ret);
				return $ret;
			}
			return null;
		}
		
		/**
		 * @return boolean
		 */
		public function TriedToPressShowResetBox() {
			if(isset($_POST[$this->m_pwShowResetButton])) {
				return true;
			}
			return false;
		}
		
		/**
		 * @return boolean
		 */
		public function TriedToPressResetPassword() {
			if(isset($_POST[$this->m_pwResetButton])) {
				return true;
			}
			return false;
		}
	}
?>